<?php
	$ROOT_RELATIVE_PATH = "../../../../../../";
	if(isset($_GET['upload_tmp_path']) && $_GET['upload_tmp_path'] != ""){
		$ROOT_UPLOAD_PATH = $_GET['upload_tmp_path'];
	} else{
		$ROOT_UPLOAD_PATH = "contents/uploads/tmp/";
	}
	
	
	$UPLOAD_PATH = realpath($ROOT_RELATIVE_PATH . $ROOT_UPLOAD_PATH);
	
	if(substr($UPLOAD_PATH, strlen($UPLOAD_PATH) - strlen("\\")) != "\\"){
		$UPLOAD_PATH = $UPLOAD_PATH . "\\";
	}
	
	$UPLOAD_PATH = str_replace("\\", "/", $UPLOAD_PATH);
	
	//echo "$UPLOAD_PATH";
	
	$action = NULL;
	if(isset($_REQUEST['action'])){
		$action = $_REQUEST['action'];
	}
	
	//echo "ACCION: $action<br/>";
	
	if($action == "upload"){		
		if(is_uploaded_file($_FILES['file']['tmp_name'])){
			if ( ! copy($_FILES['file']['tmp_name'], $UPLOAD_PATH . $_FILES['file']['name'])){
				if ( ! move_uploaded_file($_FILES['file']['tmp_name'], $UPLOAD_PATH . $_FILES['file']['name']))	{
					echo "ERROR<br/>";
				}
			}
			
			if(file_exists($UPLOAD_PATH . $_FILES['file']['name'])){
				$file = $_FILES['file']['name'];
				//problemas con las barras...
				$fileUploaded = true;
				chmod($UPLOAD_PATH . $file, 0777);
			}
		}
		
	} else if($action == "delete"){
		if(isset($_REQUEST['file'])){
			$fileDelete = $_REQUEST['file'];
			
			if(file_exists($UPLOAD_PATH . $fileDelete)){
				unlink($UPLOAD_PATH . $fileDelete);
				//echo "<br>Eliminado";
			} else{
				//echo "<br>NO EXISTE '$path$delete_file'";
			}
		}
		
	} else if($action == "delete_form"){ //Caso en el que hayamos eliminado el contenido que estabamos editando
		//No hacemos nada, solo lanzamos el evento
		
	} else if(isset($_REQUEST['file_form'])){ //Caso en el que estemos editando un contenido
		$fileForm = $_REQUEST['file_form'];	
			
	} else if(isset($_REQUEST['file'])){ //Caso en el que estemos editando un contenido
		$file = $_REQUEST['file'];	
			
	}
		
	
?>

<html>
	<head>
		<style>
			body, form{
				margin: 0;
				padding: 0;
			}
			a img{
				border: 0;				
			}
			img{
				position: relative;
				top: 3px;
			}
		</style>
		
		<script type="text/javascript" src="<?php echo $_REQUEST['static_url'];?>javascript/jquery-1.3.2.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function(){
				var baseUrl = window.parent.baseUrl;
				
				<?php if($action == "upload" && $fileUploaded): ?>				
				window.parent.set_upload_file_<?php echo $_GET['name'];?>("<?php echo $UPLOAD_PATH . $file?>");
				<?php elseif($action == "delete_form"): ?>
				window.parent.delete_form_upload_file_<?php echo $_GET['name'];?>();
				<?php elseif($action == "delete"): ?>
				window.parent.delete_upload_file_<?php echo $_GET['name'];?>();
				<?php endif;?>
				
				$("#file").change(function(){
					<?php if(isset($_GET['upload_automatic']) && $_GET['upload_automatic'] == "true"): ?>
						$("#file_loading").show();
						window.parent.set_uploading_file_<?php echo $_GET['name'];?>();
						$("form").submit();	
					<?php else:?>
						$("#file_upload").show();
					<?php endif;?>
				});
				$("#file_upload").click(function(){
					$("#file_loading").show();
					window.parent.set_uploading_file_<?php echo $_GET['name'];?>();
					$("form").submit();				
				});
				$("#file_delete").click(function(){
					$("#file_loading").show();
					$("form").submit();	
					return false;
				});

				<?php if($file != NULL): ?>
					$("#file_preview img").attr("src", baseUrl + "<?php echo $ROOT_UPLOAD_PATH . $file;?>");
					$("#file_preview img").attr("height", "50");
				<?php elseif($fileForm != NULL): ?>
					$("#file_preview img").attr("src", baseUrl + "<?php echo $fileForm;?>");
					$("#file_preview img").attr("height", "50");
				<?php endif; ?>
				$("#file_preview").click(function(){
					<?php if($file != NULL): ?>
					window.open(baseUrl + "<?php echo $ROOT_UPLOAD_PATH . $file;?>");
					<?php elseif($fileForm != NULL): ?>
					window.open(baseUrl + "<?php echo $fileForm;?>");
					<?php endif; ?>
					return false;
				});

			})
		</script>
	</head>
	
	<body>		
		<form name="upload" method="post" enctype="multipart/form-data" action="?<?php echo $_SERVER['QUERY_STRING'];?>">
			<?php if($file != NULL): ?>
				<input type="hidden" name="action" value="delete" />
				<input type="text" name="file" readonly="readonly" value="<?php echo basename($file);?>" />
				<a href="#" id="file_delete"><img src="<?php echo $_REQUEST['static_url'];?>images/delete.png" /></a>
				<a href="#" id="file_preview"><img src="<?php echo $_REQUEST['static_url'];?>images/zoom.png" /></a>
			<?php elseif($fileForm != NULL): ?>
				<input type="hidden" name="action" value="delete_form" />
				<input type="text" name="file" readonly="readonly" value="<?php echo basename($fileForm);?>" />
				<a href="#" id="file_delete"><img src="<?php echo $_REQUEST['static_url'];?>images/delete.png" /></a>
				<a href="#" id="file_preview"><img src="<?php echo $_REQUEST['static_url'];?>images/zoom.png" /></a>
			<?php else: ?>
				<input type="hidden" name="action" value="upload" />
				<input type="file" name="file" id="file"/>
				<input type="button" id="file_upload" style="display: none" value="Subir"/>				
			<?php endif; ?>
			<img id="file_loading" src="<?php echo $_REQUEST['static_url'];?>images/ajax-loader.gif" style="display: none"/>
			
			
			
		</form>
	</body>
</html>